<?php
namespace app\manager\controller;
use think\Session;
use think\Db;
use think\Cache;
class Index extends \think\Controller 
{
    public function index(){
        return $this->fetch();
    }
    /**
     * 登录处理方法
     */
    public function login(){
        if ($this->request->isPost()) {
            $user_name = $_POST['name'];
            $password = st_password($_POST['password']);
            $admin = Db::name('admin')->where(["password"=>$password,"name"=>$user_name,"state"=>1])->find();
            if($admin !== null){
                //登录成功后设置session
                Session::set('admin',$admin);
                $this->success("登录成功！", url("admin/index/index"));                
            }else {
                $this->error("登录失败，用户名或密码输入错误！", url("index/index"));
            }
        }else {
            $this->error("登录失败，请重新登录", url("index/index"));
        }
        
    }
    public function edit(){
        $admin = Session::get('admin');
        $this->assign("admin", $admin);
        return $this->fetch();
    }    

    
    public function update(){
        if ($this->request->isPost()) {
            $data     = $this->request->param();
            $password = st_password($data['password']);
            $result   = Db::name('admin')->where('id',$data['id'])->setField('password', $password);
            if($result){
                return json(['code'=>1,'msg'=>'会员更新成功！']);
            }else{
                return json(['code'=>0,'msg'=>'会员更新失败！']);
            }
        }else {
            return json(['code'=>1,'msg'=>'数据未按post方法提交！']);
        }
    }
    public function add(){
        if ($this->request->isPost()) {
            $data   = $this->request->param();
        }
        /*
         * 上传后处理功能
        if (!empty($data['photo_names']) && !empty($data['photo_urls'])) {
            $data['post']['more']['photos'] = [];
            foreach ($data['photo_urls'] as $key => $url) {
                $photoUrl = cmf_asset_relative_url($url);
                array_push($data['post']['more']['photos'], ["url" => $photoUrl, "name" => $data['photo_names'][$key]]);
            }
        }*/
    }
    /*
     * 清除session
     */
    public function clear(){
        Session::clear();
        $this->success("退出成功！", url("manager/index/index"));
    }
    public function test(){
        $str = '123456';
        echo  preg_match("/^(###)/",$str)?$str:st_password($str);
    }
    
    //发送手机验证码时需要判断发送间隔（以后完善）
    public function sendCode(){
        $param = $this->request->param();
        $account = $this->request->param('account', 0, 'string');
        $verification['code']=strval(rand(1000,9999));
        $verification['account'] =strval($account);
        //缓存时间为5分钟
        Cache::set('verification',$verification,300);
        $verification = Cache::get('verification');
        var_dump($verification);
    }
    //获取Cache数据
    public function getCode(){
        //缓存时间为5分钟
        $verification = Cache::get('verification');
        var_dump($verification);
    }
    public function checkCode(){
        $verification = Cache::get('verification');
        if ($this->request->isPost()) {
            $data   = $this->request->param();            
        }else{
            $this->error("登录失败，请重新登录", url("index/testCode"));
        }
        
        //验证码正确性检查
        if(!empty($verification['account'])&&!empty($verification['code'])&&
            ($data['phoneNumber']==$verification['account'])&&
            ($data['validateCode']==$verification['code'])){
            var_dump($data);
            var_dump($verification);
        }else {
            echo '验证码错误';
        }                  
    }
    //验证时登录界面
    public function testCode(){
        return $this->fetch();
    }

}
